<template>
	<view>
		<scroll-view class="main" scroll-y="true" :style="{height:aheight+'px'}">
			<view class="wen-name">{{name}}</view>
			<view class="main-item" v-for="(item,index) in list" :key="index">
				<view class="item-title">
					<span v-if="item.length!=1">*</span>
					<text>{{item[0]}}</text>
				</view>
				<view class="item-content flex" v-for="(items,indexs) in item" :key="indexs">
					<view class="radio flex" v-if="item.length>1 && indexs != 0">
						<text></text>
						<view>{{items}}</view>
					</view>
					<view class="flex" v-if="item.length===1">
						<input />
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="buttoncss flex">
			<button class="bts" @tap="back">返回编辑</button>
			<button class="bts bts-other" @tap="creatWenJ">确定保存</button>
		</view>
	</view>
</template>

<script>
	import api from "@/common/api.js";
	import {
		baseURL
	} from "@/common/http.js";
	import app from "../../../App.vue"
	export default {
		data() {
			return {
				list: [],
				aheight: '',
				wenid: '',
				name: '',
				dan: [],
				duo: [],
				wen: [],
				params: {
					dan: [],
					duo: [],
					wen: []
				}
			};
		},
		methods: {
			back() {
				uni.navigateTo({
					url: '../textLeadin/textLeadin?wenid=' + this.wenid
				})
			},
			// 创建问卷
			creatWenJ() {
				let _this = this
				let data = {
					api: 'wenmu',
					namewenid: this.name,
					call: '',
					url: '',
					type: 1,
					name: uni.getStorageSync('message')[0].id,
					text: '',
					wenid: ''
				}
				if (this.name && this.name != "") {
					api.createSur(data).then(res => {
						if (res.code == 200) {
							this.$store.commit('setWenJuanText', '')
							_this.wenid = res.date.wenid
							_this.save(_this.wenid)
						} else {
							uni.showToast({
								title: "创建失败",
								icon: 'none',
								duration: 1000
							})
						}
					}).catch(err => {
						console.log(err)
					})
				}
			},
			save(wenid) {
				let data = {
					api: 'wendao',
					text: [...this.dan, ...this.duo, ...this.wen],
					wenid: wenid,
					type: app.globalData.typewj
				}
				api.look(data).then(res => {
					if (app.globalData.typewj == 1) {
						uni.navigateTo({
							url: '../../editDiaocha/editDiaocha?wenid=' + this.wenid
						})
					} else if (app.globalData.typewj == 2) {
						uni.navigateTo({
							url: '../../createForm/editForm/editForm?wenid=' + this.wenid
						})
					} else {
						uni.navigateTo({
							url: '../../createVote/editVote/editVote?wenid=' + this.wenid
						})
					}
				}).catch(err => {
					console.log(err)
				})
			}
		},
		onShareAppMessage(options) {
			let shareObj = {
				success: (res) => {
					if (res.errMsg == 'shareAppMessage:ok') {
						uni.showToast({
							title: '分享成功',
							icon: 'success',
							duration: 1500
						})
					}
				},
				fail: () => {
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						uni.showToast({
							title: '取消分享',
							icon: 'none',
							duration: 2000
						})
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				},
				complete: () => {
					// 转发结束之后的回调（转发成不成功都会执行）
				}
			}
		},
		onLoad(options) {
			this.wenid = options.wenid
			let that = this
			uni.getSystemInfo({
				success(res) {
					that.aheight = res.windowHeight * 0.8
				}
			})
			//正则匹配
			let b = []
			let value = this.$store.state.wenJuanText
			let val = value.replace(/(\n\n)/g, ',')
			b = val.split(",")
			b.forEach((item, index) => {
				let c = []
				c = item.replace(/\n/g, ',')
				that.list.push(c.split(","))
			})
			this.name = that.list.splice(0, 1)[0][0]
			that.list.forEach(el => {
				if (el.length > 1) {
					if (el[0].indexOf('[多选题]') !== -1) {
						let obj = {}
						obj.di = []
						el.map((item, index) => {
							obj.name = el[0].replace('[多选题]', '')
							obj.pid = 2
							index > 0 ? obj.di.push({
								name: el[index]
							}) : ''
						})
						this.duo.push(obj)
					} else if (el[0].indexOf('[多选题]') === -1) {
						let obj = {}
						obj.di = []
						el.map((item, index) => {
							console.log(item, 'AA')
							obj.name = el[0]
							obj.pid = 1
							index > 0 ? obj.di.push({
								name: el[index]
							}) : ''
						})
						this.dan.push(obj)
					}
				} else {
					this.wen.push({
						name: el[0],
						pid: 3
					})
				}
			})
			let duoarr = this.duo
			duoarr.map((item, index) => {
				item.name = item.name.slice(2)
				return item
			})
			this.duo = duoarr
			let danarr = this.dan
			danarr.map((item, index) => {
				item.name = item.name.slice(2)
				return item
			})
			this.dan = danarr
			let wenarr = this.wen
			wenarr.map((item, index) => {
				item.name = item.name.slice(2)
				return item
			})
			this.wen = wenarr
		}
	}
</script>

<style lang="scss">
	page {
		background: #F0F0F0;
		font-weight: 500;
	}

	span {
		color: #ff0000;
	}

	.main {
		.wen-name{
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			background: #FFF;
		}
		.main-item {
			background: #fff;
			padding: 20rpx;
			margin: 0 auto 20rpx;

			.item-title {
				margin: 15rpx 0;
			}

			.item-content {
				line-height: 70rpx;

				text {
					width: 30rpx;
					height: 30rpx;
					border-radius: 50%;
					border: 1rpx solid #DFDFDF;
					margin: 0 10rpx;
				}
			}
		}
	}

	.buttoncss {
		position: fixed;
		bottom: 10%;
		left: 30rpx;
		justify-content: space-between;
		width: 690rpx;

		.bts {
			width: 320rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			color: #209FFC;
			border: 1rpx solid #209FFC;
			border-radius: 10rpx;
			background: #fff;
		}

		.bts-other {
			background: #209FFC;
			color: #fff;
		}
	}

	input {
		width: 600rpx;
		margin: 0 auto;
		border: 1rpx solid #DFDFDF;
		border-radius: 10rpx;
		height: 50rpx;
		line-height: 50rpx;
	}
</style>
